//
//  MapRadioButton.m
//  Kjs-iOS
//
//  Created by cfq on 16/6/3.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "MapRadioButton.h"

static NSMutableDictionary *_groupRadioDic = nil;

@implementation MapRadioButton

-(id)initWithDelegate:(id)delegate groupId:(NSString *)groupId backgroundColor:(UIColor *)color{
    
    self = [super init];
    
    if (self) {
        self.delegate = delegate;
        self.groupId = [groupId copy];
        [self addToGroup];
        
        self.exclusiveTouch = YES;
        
        [self setBackgroundColor:color];
        [self addTarget:self action:@selector(radioBtnChecked) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
    
}

-(id)initWithDelegate:(id)delegate groupId:(NSString *)groupId{
    
    self= [super init];
    
    if (self) {
        self.delegate = delegate;
        self.groupId = [groupId copy];
        [self addToGroup];
        
        self.exclusiveTouch = YES;
        
        //        [self setBackgroundImage:[UIImage imageNamed:@"radio-unchecked"] forState:UIControlStateNormal];
        //        [self setBackgroundImage:[UIImage imageNamed:@"radio-checked"] forState:UIControlStateSelected];
        [self setImage:[UIImage imageNamed:@"radio-unchecked"] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:@"radio-checked"] forState:UIControlStateSelected];
        [self addTarget:self action:@selector(radioBtnChecked) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return self;
}
-(id)initWithDelegate:(id)delegate groupId:(NSString *)groupId normalBackgroundImage:(NSString *)normalBackgroundImage selectedBackgroundImage:(NSString *)selectedBackgroundImage{
    
    
    self= [super init];
    
    if (self) {
        self.delegate = delegate;
        self.groupId = [groupId copy];
        [self addToGroup];
        
        self.exclusiveTouch = YES;
        
        [self setBackgroundImage:[UIImage imageNamed:normalBackgroundImage] forState:UIControlStateNormal];
        [self setBackgroundImage:[UIImage imageNamed:selectedBackgroundImage] forState:UIControlStateSelected];
        
        [self addTarget:self action:@selector(radioBtnChecked) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
    
}
- (void)addToGroup {
    if(!_groupRadioDic){
        _groupRadioDic = [[NSMutableDictionary dictionary] init];
    }
    
    NSMutableArray *_gRadios = [_groupRadioDic objectForKey:_groupId];
    if (!_gRadios) {
        _gRadios = [NSMutableArray array];
    }
    [_gRadios addObject:self];
    [_groupRadioDic setObject:_gRadios forKey:_groupId];
}
- (void)removeFromGroup {
    if (_groupRadioDic) {
        NSMutableArray *_gRadios = [_groupRadioDic objectForKey:_groupId];
        if (_gRadios) {
            [_gRadios removeObject:self];
            if (_gRadios.count == 0) {
                [_groupRadioDic removeObjectForKey:_groupId];
            }
        }
    }
}
- (void)uncheckOtherRadios {
    NSMutableArray *_gRadios = [_groupRadioDic objectForKey:_groupId];
    if (_gRadios.count > 0) {
        for (MapRadioButton *_radio in _gRadios) {
            if (_radio.checked && ![_radio isEqual:self]) {
                _radio.checked = NO;
            }
        }
    }
}
- (void)setChecked:(BOOL)checked {
    if (_checked == checked) {
        return;
    }
    
    _checked = checked;
    self.selected = checked;
    
    if (self.selected) {
        [self uncheckOtherRadios];
    }
    
    if (self.selected && _delegate && [_delegate respondsToSelector:@selector(didSelectedRadioButton:groupId:)]) {
        [_delegate didSelectedRadioButton:self groupId:_groupId];
    }
}
- (void)radioBtnChecked {
    if (_checked) {
        return;
    }
    
    self.selected = !self.selected;
    _checked = self.selected;
    
    if (self.selected) {
        [self uncheckOtherRadios];
    }
    
    if (self.selected && _delegate && [_delegate respondsToSelector:@selector(didSelectedRadioButton:groupId:)]) {
        [_delegate didSelectedRadioButton:self groupId:_groupId];
        
    }
}

@end
